<?php include('cms.php');  Cms::nocache(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Attachments</title>
<link rel="stylesheet" type="text/css" href="css/custom-theme/jquery-ui-1.8.1.custom.css"/>
<link rel="stylesheet" type="text/css" href="css/cms.css"/>
<link rel="stylesheet" type="text/css" href="css/editor.css"/>
<script type="text/javascript" src="strings.php?js"></script>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.1.custom.min.js"></script>
<style>
	
	body{ margin:0; padding:0; background:white; }
	
	#frmUploader { 
		display:none; 
		background:#FFCC7F;
		border:solid 1px #F90; 
		padding:.5em;
	}
	
	form input[name="file"]{
		display:block;
	}
	
	#files { 
		display:block;
		list-style:none;
		margin:0;
		padding:0;
	}
	
	#files li{
		display:block;
		clear:both;
		margin:0px 0;
		padding:0 0 0 0px;
	}
	
	#files button {  
		display:block;
		width:100%;
		background:none;
		border:none;
		text-align:left;
		margin:0;
		font-size:10pt;
		padding:5px 0;
	}
	
	#files .icon-grip{
		float:left;
	}
	
	#files button:hover {
		background: #f0f0f0;
	}
	
	#files button.selected{
		background:#e0e0e0;
	}
	
	#files .icon{
		margin:5px 6px -3px 5px;
	}
	
	#files .fileMenu .icon{
		margin-left:15px;
	}
	
	#files .id, #files .path{ display: none; }
	
	.fileMenu{
		border:solid 1px #e0e0e0;
		display:none;
	}
	
</style>
<script>
	

	function makeicon(cls){
		return $('<div>').addClass('icon').addClass('icon-' + cls);	
	}
	
	function extension(name){
		if(name.indexOf('.') >= 0){
			return name.substr(name.lastIndexOf('.') + 1 ).toLowerCase();
		}else{
			return "";
		}
	}
	
	function isimage(name){
		var ext = extension(name);
		
		return ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "png";
	}

	function createMenu(name){
		
		return $('<div>')
			.addClass('fileMenu')
			.append( $('<button>').append(makeicon('link2')).append(strings.makeLink).click(function(){ makeLink(this); }) )
			.append( $('<button>').append(makeicon('rename')).append(strings.renameFile).click(function(){ renameFile(this); }) )
			.append( (isimage(name) ?  
							  $('<button>').append(makeicon('embed')).append(strings.embed).click(function(){ embed(this); })
							  : '') )
			.append( $('<button>').append(makeicon('download')).append(strings.downloadFile).click(function(){ downloadFile(this); }) )
			.append( $('<button>').append(makeicon('remove')).append(strings.deleteFile).click(function(){ deleteFile(this); }) )
			//.find('button')
				//.mousedown(function(evt){ e.preventDefault(); })
				;
		
	}
	
	function findid(button){
		return $(button).parent().parent().find('.id').text();
	}
	
	function findname(button){
		return $(button).parent().parent().find('.file').text();
	}
	
	function findpath(button){
		return $(button).parent().parent().find('.path').text();
	}
	
	function makeLink(button){
		window.parent.insertAttachmentLink(findid(button), findname(button));
	}
	
	function embed(button){
		window.parent.embedAttachment(findpath(button), findname(button));
	}
	
	function renameFile(button){
		
		var name = findname(button);
		var id = findid(button);
		var ext = extension(name);
		
		
		var newname = window.prompt(strings.enterNewFileName, name);
		
		if(!newname) return;
		
		newname = newname + '.' + ext;
		
		window.parent.status(strings.renamingFile, true);
		
		$.post('attchs.modify.php', { op: 'rename', idfile: id, newname: newname },
			function(data){
				if(data == 'OK'){
					document.location = document.location;
				}else{
					alert(strings.errorRenamingFile + data);
				}
				window.parent.status();
			});
	}
	
	function deleteFile(button){
		
		var id = findid(button);
		var name = findname(button);
		
		if(!confirm(strings.doYouWantToDeleteFile + name + '?' )) 
			return;
		
		window.parent.status(strings.deleteingFile, true);
		
		$.post('attchs.modify.php', { op: 'delete', idfile: id},
			function(data){
				if(data == 'OK'){
					document.location = document.location;
				}else{
					alert(strings.errorDeleteingFile + data);
				}
				window.parent.status();
			});
		
	}
	
	function downloadFile(button){
		
		var id = findid(button);
		
		document.location = 'download.php?id=' + id;
	}
	
	$(function(){
		$('.icon-newfile').parent()
			.click(function(){
				if($('#frmUploader').is(':visible')){
					$('#frmUploader').hide(200);   
				}else{
					$('#frmUploader').show(200);			   
				}
			});
			
		$('#file')
			.change(function(){
				$('#frmUploader').submit();
			});
		
		$('#files').sortable();
		
		$('#files .file')
			.click(function(){ 
				
				var closing = false;
				
				if($(this).hasClass('selected')) closing = true;
				
				$('button.selected').removeClass('selected');
				$('.fileMenu')
					.hide(200, function(){ $(this).remove(); })
				
				if(!closing){
					$(this)
						.addClass('selected')
						.parent().append(createMenu($(this).text()));
						
					$('.fileMenu').show(200);
				}
				
			});
			
			$('button').mousedown(function(evt){ evt.preventDefault(); })
	});
	
</script>
</head>
<body>
    <div class="toolbar">
    	<button><div class="icon icon-newfile"></div></button>
    </div>
    <div class="uploader">
    <form id="frmUploader" method="post" enctype="multipart/form-data" action="attchs.upload.php">
    	<input type="hidden" name="idcontent" value="<?=$_GET['idcontent']?>" />
    	<input type="file" name="file" id="file" />
    </form>
    </div>
    <ul id="files">
    	<?php
		
			$atts = DataLatte::arrayof('Attachment', "SELECT * FROM cmsAttachment WHERE cmsContent_idcontent = '" . $_GET['idcontent'] . "'");
        
			foreach($atts as $a){
				echo "<li><button class=file><div class='icon " . Attachment::fileCss($a->name) . "'></div>$a->name</button><div class='id'>$a->idattachment</div><div class='path'>$a->path</div></li>";
			}
		?>
    </ul>
</body>
</html>